//
//  HZSelectedStockView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/9.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZSelectedStockView.h"
#import "HZOwnedStockTableViewCell.h"
#import "MYUpDownBtn.h"

#define kOwnedStockCellH 59

@interface HZSelectedStockView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView    *pTableView;
@property (nonatomic, strong)NSArray        *pListData;
@property (nonatomic, strong)UIView         *pTitleBgView;
@property (nonatomic, strong)UILabel        *pNameLabel;
@property (nonatomic, strong)UIButton       *pPriceLabel;
@property (nonatomic, strong)UIButton       *pZhangDieLabel;
@property (nonatomic, strong)UIButton       *pZhangFuLabel;

@property (nonatomic, strong)UIView *pLine1;
@property (nonatomic, strong)UIView *pLine2;
@property (nonatomic, strong)UIView *pLine3;
@property (nonatomic, strong)UIView *pLine4;

@property (nonatomic, assign)kOrder_type nPriceType;
@property (nonatomic, assign)kOrder_type nZhangDieType;
@property (nonatomic, assign)kOrder_type nZhangFuType;

@property (nonatomic, strong)MYUpDownBtn *pAlertInfo;

@property (nonatomic, strong)NSArray *pNormalOrderListData;//默认排序缓存
@end

@implementation HZSelectedStockView
- (void)p_loadAlertInfo
{
    if (!_pAlertInfo) {
        _pAlertInfo = [MYUpDownBtn new];
        [_pAlertInfo setImage:[UIImage imageNamed:@"ownLogin"] forState:UIControlStateNormal];
        [_pAlertInfo setImage:[UIImage imageNamed:@"ownLogin"] forState:UIControlStateHighlighted];
        [_pAlertInfo setImage:[UIImage imageNamed:@"search"] forState:UIControlStateSelected];
        _pAlertInfo.imageView.contentMode = UIViewContentModeCenter;
//        [_pAlertInfo setTitle:@"请登录账户以获取自选股服务" forState:UIControlStateNormal];
        [_pAlertInfo setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
        _pAlertInfo.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_pAlertInfo];
        [_pAlertInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.centerY.mas_equalTo(self).offset(-50);
            make.width.mas_equalTo(self);
            make.height.mas_equalTo(110);
        }];
        [_pAlertInfo addTarget:self action:@selector(alertBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    _pAlertInfo.hidden = NO;
    _pAlertInfo.selected = NO;
//    [_pAlertInfo setTitle:@"请登录账户以获取自选股服务" forState:UIControlStateNormal];
}
#pragma mark - 
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    _pTitleBgView = [UIView new];
    _pTitleBgView.backgroundColor = kMYHexToRGB(0xffffff, 1);
    _pNameLabel = [UILabel new];
    _pPriceLabel = [UIButton new];
    _pZhangFuLabel = [UIButton new];
    _pZhangDieLabel = [UIButton new];
    _pLine1 = [UIView new];
    _pLine2 = [UIView new];
    _pLine3 = [UIView new];
    _pLine4 = [UIView new];

    _pNameLabel.text = kHZCodeName;
    [_pPriceLabel setTitle:kHZCodePrice forState:UIControlStateNormal];
    [_pZhangFuLabel setTitle:kHZZhangFu forState:UIControlStateNormal];
    [_pZhangDieLabel setTitle:kHZZhangDie forState:UIControlStateNormal];
    [_pPriceLabel addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_pZhangFuLabel addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_pZhangDieLabel addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_pPriceLabel setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
    [_pZhangDieLabel setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
    [_pZhangFuLabel setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
    
    _pNameLabel.textAlignment = NSTextAlignmentCenter;
    _pNameLabel.textColor = kMYHexToRGB(0x888888, 1);
    
    _pLine4.backgroundColor = kMYHexToRGB(0x888888, 1);
    _pLine1.backgroundColor = _pLine2.backgroundColor = _pLine3.backgroundColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:kMYHexToRGB(0xdedede, 1)];

    
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.dataSource = self;
    _pTableView.delegate = self;
    _pTableView.tableFooterView = [UIView new];
    
    [self addSubview:_pTitleBgView];
    [self addSubview:_pTableView];
    [_pTitleBgView addSubview:_pNameLabel];
    [_pTitleBgView addSubview:_pPriceLabel];
    [_pTitleBgView addSubview:_pZhangFuLabel];
    [_pTitleBgView addSubview:_pZhangDieLabel];
    [_pTitleBgView addSubview:_pLine1];
    [_pTitleBgView addSubview:_pLine2];
    [_pTitleBgView addSubview:_pLine3];
    [_pTitleBgView addSubview:_pLine4];

    
    [self p_initConstraints];
    
    if (![HZUserManager manager].isLogined) {
        [self p_loadAlertInfo];
    }else if ([HZUserManager manager].isLogined && ![HZUserManager manager].isDownLoadStock) {
        [self p_loadAlertInfo];
        [self updateAlertInfo];
    }else {
        _pAlertInfo.hidden = YES;
    }
}

#pragma mark -
#pragma mark private
- (void)p_initConstraints
{
    [_pTitleBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(45);
        make.bottom.mas_equalTo(_pTableView.mas_top);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
    [_pNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTitleBgView);
        make.left.mas_equalTo(_pTitleBgView);
        make.right.mas_equalTo(_pPriceLabel.mas_left);
        make.bottom.mas_equalTo(_pTitleBgView);
    }];
    [_pPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTitleBgView);
        make.right.mas_equalTo(_pZhangDieLabel.mas_left);
        make.bottom.mas_equalTo(_pTitleBgView);
    }];
    [_pZhangDieLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTitleBgView);
        make.right.mas_equalTo(_pZhangFuLabel.mas_left);
        make.bottom.mas_equalTo(_pTitleBgView);
    }];
    [_pZhangFuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTitleBgView);
        make.right.mas_equalTo(_pTitleBgView);
        make.bottom.mas_equalTo(_pTitleBgView);
        make.width.mas_equalTo(@[_pNameLabel,_pPriceLabel,_pZhangDieLabel]);
    }];
    
    [_pLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_pTitleBgView);
        make.centerX.mas_equalTo(_pNameLabel.mas_right);
        make.height.mas_equalTo(12);
        make.width.mas_equalTo(0.5);
    }];
    [_pLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_pTitleBgView);
        make.centerX.mas_equalTo(_pPriceLabel.mas_right);
        make.height.mas_equalTo(_pLine1);
        make.width.mas_equalTo(_pLine1);
    }];
    [_pLine3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_pTitleBgView);
        make.centerX.mas_equalTo(_pZhangDieLabel.mas_right);
        make.height.mas_equalTo(_pLine1);
        make.width.mas_equalTo(_pLine1);
    }];
    [_pLine4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_pTitleBgView);
        make.height.mas_equalTo(0.5);
    }];
    
}
- (void)updateAlertInfo
{
    _pAlertInfo.selected = YES;
    [_pAlertInfo setTitle:@"正在同步自选股..." forState:UIControlStateNormal];
}
- (void)updateThemeColor
{
    _pTableView.backgroundColor = [HZCommonTool getThemeColor];
    _pTitleBgView.backgroundColor = [HZCommonTool getThemeColor];
    [_pTableView reloadData];
    _pTableView.separatorColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:nil];
    _pLine4.backgroundColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:nil];
    _pLine1.backgroundColor = _pLine2.backgroundColor = _pLine3.backgroundColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:kMYHexToRGB(0xdedede, 1)];
}
- (void)alertBtnOnClick
{
    [self.parent upToParentWithObjc:self withType:KMSClickLoginAndDownLoadOwnStockType withParamter:nil];
}
- (void)btnOnClick:(UIButton *)btn
{
    NSString *title = btn.currentTitle;
    if ([title isEqual:kHZCodePrice]) {
        _nZhangFuType = _nZhangDieType = kOrder_normal_type;
        [_pZhangDieLabel setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        [_pZhangFuLabel setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        if (_nPriceType == kOrder_normal_type) {
            _nPriceType = kOrder_positive_type;
            [btn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
        }else if (_nPriceType == kOrder_positive_type) {
            _nPriceType = kOrder_negative_type;
            [btn setImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateNormal];
        }else if (_nPriceType == kOrder_negative_type) {
            _nPriceType = kOrder_normal_type;
            _pListData = _pNormalOrderListData;
            [btn setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        }
    }else if ([title isEqual:kHZZhangDie]) {
        _nPriceType = _nZhangFuType = kOrder_normal_type;
        [_pPriceLabel setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        [_pZhangFuLabel setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        if (_nZhangDieType == kOrder_normal_type) {
            _nZhangDieType = kOrder_positive_type;
            [btn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
        }else if (_nZhangDieType == kOrder_positive_type) {
            _nZhangDieType = kOrder_negative_type;
            [btn setImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateNormal];
        }else if (_nZhangDieType == kOrder_negative_type) {
            _nZhangDieType = kOrder_normal_type;
            _pListData = _pNormalOrderListData;
            [btn setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        }
    }else if ([title isEqual:kHZZhangFu]) {
        _nPriceType = _nZhangDieType = kOrder_normal_type;
        [_pZhangDieLabel setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        [_pPriceLabel setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        if (_nZhangFuType == kOrder_normal_type) {
            _nZhangFuType = kOrder_positive_type;
            [btn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
        }else if (_nZhangFuType == kOrder_positive_type) {
            _nZhangFuType = kOrder_negative_type;
            [btn setImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateNormal];
        }else if (_nZhangFuType == kOrder_negative_type) {
            _nZhangFuType = kOrder_normal_type;
            _pListData = _pNormalOrderListData;
            [btn setImage:[UIImage imageNamed:@"arrow_none"] forState:UIControlStateNormal];
        }
    }
    [self p_updateNewData:_pListData];
}
- (NSArray *)p_packageSortedWithData:(NSArray *)data
{
    NSArray *newData = data;
    if (_nPriceType != kOrder_normal_type) {
        newData = [data sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            HZOwnedStockModel *model1 = (HZOwnedStockModel *)obj1;
            HZOwnedStockModel *model2 = (HZOwnedStockModel *)obj2;
                    if (_nPriceType == kOrder_positive_type) {
                        if ([model1.model_Price floatValue] > [model2.model_Price floatValue]) {
                            return NSOrderedAscending;
                        }else if ([model1.model_Price floatValue] < [model2.model_Price floatValue]) {
                            return NSOrderedDescending;
                        }else {
                            return NSOrderedSame;
                        }
                    }else if (_nPriceType == kOrder_negative_type) {
                        if ([model1.model_Price floatValue] > [model2.model_Price floatValue]) {
                            return NSOrderedDescending;
                        }else if ([model1.model_Price floatValue] < [model2.model_Price floatValue]) {
                            return NSOrderedAscending;
                        }else {
                            return NSOrderedSame;
                        }
                    }else {
                        return NSOrderedSame;
                    }
                }];
    }else if (_nZhangDieType != kOrder_normal_type) {
        newData = [data sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            HZOwnedStockModel *model1 = (HZOwnedStockModel *)obj1;
            HZOwnedStockModel *model2 = (HZOwnedStockModel *)obj2;
            if (_nZhangDieType == kOrder_positive_type) {
                if ([model1.model_ZhangDie floatValue] > [model2.model_ZhangDie floatValue]) {
                    return NSOrderedAscending;
                }else if ([model1.model_ZhangDie floatValue] < [model2.model_ZhangDie floatValue]) {
                    return NSOrderedDescending;
                }else {
                    return NSOrderedSame;
                }
            }else if (_nZhangDieType == kOrder_negative_type) {
                if ([model1.model_ZhangDie floatValue] > [model2.model_ZhangDie floatValue]) {
                    return NSOrderedDescending;
                }else if ([model1.model_ZhangDie floatValue] < [model2.model_ZhangDie floatValue]) {
                    return NSOrderedAscending;
                }else {
                    return NSOrderedSame;
                }
            }else {
                return NSOrderedSame;
            }
        }];
    }else if (_nZhangFuType != kOrder_normal_type) {
        newData = [data sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            HZOwnedStockModel *model1 = (HZOwnedStockModel *)obj1;
            HZOwnedStockModel *model2 = (HZOwnedStockModel *)obj2;
            if (_nZhangFuType == kOrder_positive_type) {
                if ([model1.model_ZhangFu floatValue] > [model2.model_ZhangFu floatValue]) {
                    return NSOrderedAscending;
                }else if ([model1.model_ZhangFu floatValue] < [model2.model_ZhangFu floatValue]) {
                    return NSOrderedDescending;
                }else {
                    return NSOrderedSame;
                }
            }else if (_nZhangFuType == kOrder_negative_type) {
                if ([model1.model_ZhangFu floatValue] > [model2.model_ZhangFu floatValue]) {
                    return NSOrderedDescending;
                }else if ([model1.model_ZhangFu floatValue] < [model2.model_ZhangFu floatValue]) {
                    return NSOrderedAscending;
                }else {
                    return NSOrderedSame;
                }
            }else {
                return NSOrderedSame;
            }
        }];
    }else {
        _pNormalOrderListData = data;
    }
    
    return newData;
}

- (void)p_updateNewData:(NSArray *)data
{
    if (![HZUserManager manager].isLogined) {
        [self p_loadAlertInfo];
        if ([data isKindOfClass:[NSArray class]]) {
            self.pListData = data;
            [_pTableView reloadData];
        }
        return;
    }
    if (data && [data isKindOfClass:[NSArray class]]) {
        _pAlertInfo.hidden = YES;
        self.pListData = [self p_packageSortedWithData:data];
        [_pTableView reloadData];
    }
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.pListData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HZOwnedStockModel *model = _pListData[indexPath.row];
    HZOwnedStockTableViewCell *cell = [HZOwnedStockTableViewCell ownedStockCellTableView:tableView indexPath:indexPath cellModel:model cellHight:kOwnedStockCellH];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kOwnedStockCellH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZOwnedStockModel *model = _pListData[indexPath.row];
    [self.parent upToParentWithObjc:self withType:kHZ_jumpSingleStock_Action withParamter:model];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_updateNewData_Action:
        {
            [self p_updateNewData:paramter];
        }
            break;
            
        default:
            break;
    }
}

@end
